package com.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.entity.ClassAndUser;
import com.entity.ClassEvaluation;
import com.entity.ClassInfo;
import com.entity.EvaItems;
import com.entity.User;
import com.mapper.ClassMapper;
import com.service.ClassService;

public class ClassServiceImpl implements ClassService {
	@Autowired
	private ClassMapper classMapper;

	@Override
	public List<ClassInfo> getClassInfoByUserId(User user) throws Exception {
		return classMapper.getClassInfoByUserId(user);
	}
	
	@Override
	public List<ClassEvaluation> getClassEvaluationInfoByUserId(User user) throws Exception {
		return classMapper.getClassEvaluationInfoByUserId(user);
	}
	
	@Override
	public List<ClassEvaluation> getClassEvaluationInfoForTea(User user) throws Exception {
		List<ClassEvaluation> l = classMapper.getClassEvaluationInfoForTea(user);
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh-mm-ss"); 
		for(ClassEvaluation e : l){
			String str=sdf.format(e.getAddTime()); 
			e.setStrTime(str);
		}
		return l;
	}
	
	@Override
	public List<ClassInfo> getAllClassInfo() throws Exception {
		return classMapper.getAllClassInfo();
	}
	
	@Override
	public Integer getAllEvaPeople(Long classId) throws Exception {
		return classMapper.getAllEvaPeople(classId);
	}
	
	@Override
	public List<ClassEvaluation> getEvainfo(Long classId) throws Exception {
		return classMapper.getEvainfo(classId);
	}
	
	@Override
	public void addClass(String name,String class_introduce,Integer class_sum) throws Exception {
		ClassInfo classInfo = new ClassInfo();
		classInfo.setName(name);
		classInfo.setClass_introduce(class_introduce);
		classInfo.setClass_sum(class_sum);
		classMapper.addClass(classInfo);
	}
	
	@Override
	public Map<String, Object> getImportClassDetails(List<List<String>> rows) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		boolean isSubmit = true;
		List<ClassInfo> classList = new ArrayList<ClassInfo>();
		for (int i = 0; i < rows.size(); i++) {
			List<String> excelRow = rows.get(i);
			if (i == 0) { // 第一行是表头
				continue;
			}
			ClassInfo ci = new ClassInfo();
			if (excelRow.size() != 3) {
				ci.setErrorTips("导入的文件列数不等于2列!");
				isSubmit = false;
			} else {
				ci.setName(excelRow.get(0));//课程名称
				ci.setClass_introduce(excelRow.get(1));//课程简介
				ci.setClass_sum(Integer.parseInt(excelRow.get(2)));//课程总数
				classList.add(ci);
			}
		}
		map.put("isSubmit", isSubmit);
		map.put("classList", classList);
		return map;
	}
	
	@Override
	public List<EvaItems> getEvaItems(EvaItems evaItems) throws Exception {
		return classMapper.getEvaItems(evaItems);
	}
	
	@Override
	public Integer modEI(EvaItems ei) throws Exception {
		return classMapper.modEI(ei);
	}
	
	@Override
	public void delEI(Long id) throws Exception {
		classMapper.delEI(id);
	}
	
	@Override
	public void addEI(EvaItems ei) throws Exception {
		classMapper.addEI(ei);
		
	}
	
	@Override
	public void addClassAndUser(ClassAndUser cu) throws Exception {
		classMapper.addCU(cu);
	}
}
